<?php

namespace App\Http\Controllers;

use App\Http\Response\CodeMessage;
use App\Models\Admin;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;

class BaseAdminController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    // protected $auth = 'auth:api';  //非admin时 使用.这个会在 执行php artisan route:list 后，在Middleware的屁股后加上api
    protected $auth = 'auth:admin';  //这个会在 执行php artisan route:list 后，在Middleware的屁股后加上admin,同时也是条用 登录验证 中间件的关键
    protected ?Admin $admin = null;

    public function __construct()
    {
        parent::__construct();
        //下面这个 获取Config/auth.php中guards数组中 admin的值
        $this->admin = auth()->guard('admin')->user();
        // dump($this->admin);
    }

    protected function returnJson($errno, $message, $data = null, $http_status = 200, array $headers = [], $options = 0)
    {
        $ret = [
            'code' => $errno,
            'message' => $message
        ];
        if ($data instanceof JsonResource) {
            return $data->additional($ret);
        }
        $ret['data'] = $data;
        return response()->json($ret, $http_status, $headers, $options);
    }

    protected function noContent($message = 'No content')
    {
        return $this->returnJson(null, $message, null, Response::HTTP_NO_CONTENT);
    }

    // 数据请求成功 数据优先
    protected function success($data = [], $message = 'success')
    {
        return $this->returnJson(0, $message, $data);
    }

    // 操作成功 提示优先
    protected function successed($message = 'success', $data = [])
    {
        return $this->returnJson(0, $message, $data);
    }

    // 附带其他参数
    protected function successWith($data = [], $extension = [], $message = 'success', $http_status = 200, array $headers = [], $options = 0)
    {
        $ret = [
            'code' => 0,
            'message' => $message
        ];
        $ret = array_merge($ret, $extension);
        if ($data instanceof JsonResource) {
            return $data->additional($ret);
        }
        $ret['data'] = $data;
        return response()->json($ret, $http_status, $headers, $options);
    }

    protected function create($data = null, $message = 'create', string $location = '')
    {
        $headers = [];
        if ($location) {
            $headers = ['Location' => $location];
        }
        return $this->returnJson(0, $message, $data, Response::HTTP_CREATED, $headers);
    }

    protected function created($message = 'created', $data = null, string $location = '')
    {
        $headers = [];
        if ($location) {
            $headers = ['Location' => $location];
        }
        return $this->returnJson(0, $message, $data, Response::HTTP_CREATED, $headers);
    }

    protected function fail($err_msg = null, $code_msg = CodeMessage::OPERATION_FAILURE, $http_status = 200)
    {
        [$err_code, $msg] = $code_msg;
        $err_msg = is_null($err_msg) ? $msg : $err_msg;
        return $this->returnJson($err_code, $err_msg, null, $http_status);
    }
}
